require(["config"],function(){
	require(["jquery","template","docHeader","cookie"],function($,template){
		function Close(){
			this.load();
			this.addListener();
			this.loadAddress();
			this.verify();
		}
		
		$.extend(Close.prototype,{
			verify: function() {
				$.cookie.json = true;
				var user = $.cookie("login-user");
				if (!user) // 用户未登录
					location = "/html/login.html";
			},
			load:function(){
				// 配置 cookie 插件自动转换 JS 与 JSON
				$.cookie.json=true;
				// 读取cookie中保存的购物车
				var products=this.products=$.cookie("cars")||[];
				//渲染模板
				var data={products:products};
//				console.log(data);
				var html=template("cart-body-template",data);
				$(".cart-table tbody").html(html);
				//计算合计
				this.calcTotal();
				//购物车为空时
				if (this.products.length==0){
					$("#docSettlement .show").hide();
					$("#docSettlement .empty").show();
					return;
				}
				//购物车不为空
				$("#docSettlement .empty").hide().siblings(".show").removeClass("hide");
			},
			//计算合计
			calcTotal:function(){
				var sum=0;
				$("#docSettlement .sub").each(function(index,element){
					sum+=Number($(this).text());
				})
//				console.log(sum);
				$("#docSettlement .mmtotal").text(sum);
			},
			addListener:function(){
				$("#docSettlement .shouye").on("click",$.proxy(this.shouyeReturn,this));
				$("#docSettlement .detail").on("click",$.proxy(this.detailReturn,this));
			},
			shouyeReturn:function(){
				window.location="/";
			},
			detailReturn:function(){
				window.location="/html/detail.html";
			},
			//加载地址数据
			loadAddress:function(){
				//首先加载省份数据
				this.loadProvince();
				//省份选择改变时，加载城市
				$(".province").on("change",this.loadCity);
				//城市选择改变时，加载区县
				$(".city").on("change",this.loadDistrict);
			},
			//加载省份
			loadProvince:function(){
				var url1="http://route.showapi.com/1149-1?showapi_appid=75548&showapi_sign=aff70eb718594bb8b8a46c63e0194a32&level=1&page=1&maxSize=20",
					url2="http://route.showapi.com/1149-1?showapi_appid=75548&showapi_sign=aff70eb718594bb8b8a46c63e0194a32&level=1&page=2&maxSize=20";
				$.when($.ajax(url1),$.ajax(url2))
					.done(function(data1,data2){
//						console.log(arguments);
						var html='<option value="-1">请选择省份</option>';
						data1[0].showapi_res_body.data
						.concat(data2[0].showapi_res_body.data)
						.forEach(function(curr){
							html+=`<option value="${curr.id}">${curr.areaName}</option>`;
						});
						$(".province").html(html);
					});
			},
			//加载城市
			loadCity:function(){
				//获取选择的省份id
				var id=$(".province").val();
//				console.log(id);
				//根据id查找城市
				var url=`http://route.showapi.com/1149-2?showapi_appid=75548&showapi_sign=aff70eb718594bb8b8a46c63e0194a32&parentId=${id}`;
				$.ajax(url).done(function(data){
					var html='<option value="-1">请选择城市</option>';
					data.showapi_res_body.data.forEach(function(curr){
						html+=`<option value="${curr.id}">${curr.areaName}</option>`;
					});
					$(".city").html(html);
				});
			},
			// 加载区县
			loadDistrict: function() {
				// 获取选择的城市 id
				var id = $(".city").val();
				// 根据 id 查询区县
				var url = `http://route.showapi.com/1149-2?showapi_appid=75548&showapi_sign=aff70eb718594bb8b8a46c63e0194a32&parentId=${id}`;
				$.ajax(url).done(function(data) {
					var html = '<option value="-1">请选择区县</option>';
					data.showapi_res_body.data.forEach(function(curr) {
						html += `<option value="${curr.id}">${curr.areaName}</option>`;
					});
					$(".district").html(html);
				});
			}
		});
		
		new Close();
	});
});
